package utils
{
import flash.utils.Dictionary;
import flash.utils.describeType;
import flash.utils.getQualifiedClassName;

import values.avatar.AvatarTypeValue;
import values.avatar.AvatarTypeValueList;
import values.avatar.AvatarValue;
import values.item.EquipmentItemValue;

/**
 * EquipUtil
 * @author jliang
 *
 */
public class EquipUtil
{
	private static var defMap:Dictionary = new Dictionary(true);   /* XML */
    
    public static function getSupportAvatarType(equipType:String):Array
    {
        var types:Array = [];
        for each (var type:AvatarTypeValue in AvatarTypeValueList.avatarTypes)
        {
            if (type.canSupportEquip(equipType))
            {
                types.push(type.key);
            }
        }
        return types;
    }
	
	/**
	 * 处理装备所影响的角色属性
	 * @param avatar
	 * @param item
	 * @param flag              1 -> add;   -1 -> remove
	 *  
	 */	
	public static function handleEquipmentProperty(avatar:AvatarValue, item:EquipmentItemValue, flag:int = 1):void
	{
		var ref:String = getQualifiedClassName(item);
		
		var metadataList:Vector.<Object> = defMap[ref];
		
		if (!metadataList)
		{
			metadataList = new Vector.<Object>();
			
			var def:XML = describeType(item);
			var accessors:XMLList = def.accessor;
			
			for each (var acc:* in accessors)
			{
				var propName:String = String(acc.@name);
				var metadata:* = acc.metadata.(@name == "Prop");
				if (metadata.toXMLString() != "")
				{
					var mapping:String = String(metadata.arg.(@key == "mapping").@value);
					
					metadataList.push( { mapping: mapping, prop:propName } );
				}
			}
			
			defMap[ref] = metadataList;
		}
		
		for each (var mdObj:Object in metadataList)
		{
			avatar[mdObj.mapping] = avatar[mdObj.mapping] + flag * item[mdObj.prop];
		}
	}
}
}



